package com.xckj.teacher.framework

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.DialogFragment
import com.gyf.immersionbar.ImmersionBar
import com.xckj.teacher.R
import com.xckj.teacher.view.LoadingDialog

/**
 * @Describe: DialogFragment的基类
 */
abstract class BaseDialogFragment<T>(var type: Int) : DialogFragment(), IBaseView {
    protected var mContext: Context? = null
    protected var dataBinding: T? = null
    private var loadingDialog: LoadingDialog? = null
    private var dialogCount = 0
    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        if (dialog == null) {
            return super.onCreateView(inflater, container, savedInstanceState)
        }
        val window = dialog!!.window
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.decorView?.setPadding(0, 0, 0, 0)
        val wlp = window!!.attributes
        if (type == 1) {
            wlp.gravity = Gravity.BOTTOM
        } else {
            wlp.gravity = Gravity.CENTER
        }
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT
        window.setAttributes(wlp)
        val view = LayoutInflater.from(mContext).inflate(loadLayout(), null)
        dataBinding = DataBindingUtil.bind<ViewDataBinding>(view) as T?
        initData()
        return view
    }

    /**
     * 加载布局
     *
     * @return 布局Id
     */
    protected abstract fun loadLayout(): Int

    /**
     * 初始化数据
     */
    protected abstract fun initData()


    override fun showMessage(message: String) {
        if (!TextUtils.isEmpty(message)) {
            requireActivity().runOnUiThread {
                Toast.makeText(
                    requireActivity(),
                    message,
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }

    override fun showLoading() {
        requireActivity().runOnUiThread {
            dialogCount++
            if (null == loadingDialog || !loadingDialog!!.isShowing) {
                loadingDialog = LoadingDialog(requireActivity())
                loadingDialog!!.create()
            }
            if (!loadingDialog!!.isShowing) {
                loadingDialog!!.show()
            }
        }
    }

    /**
     * 隐藏Loading
     */
    override fun hideLoading() {
        requireActivity().runOnUiThread {
            dialogCount--
            if (null == loadingDialog) {
                return@runOnUiThread
            }
            if (dialogCount <= 0) {
                loadingDialog!!.dismiss()
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (loadingDialog != null) {
            loadingDialog!!.dismiss()
        }
    }
}
